package org.vectrics.common.struts;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Locale;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.VectricsException;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.web.menu.Menu;
import org.vectrics.web.menu.MenuItem;

public abstract class MenuBaseTag extends VectricsTagSupport {
    static private transient Logger log = Logger.getLogger(MenuBaseTag.class);

    static private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");

    private String selectItemId = "";

    public static String TABLE_FONT = "subFont";

    private HttpSession session = null;

    private String contextPath = null;

    public MenuBaseTag() {
        super();
    }

    protected abstract Menu getMenu() throws VectricsException;

    protected Collection getAllMenus() {
        return CoreServiceLocator.getConfiguration().getAllMenus();
    }

    @Override
    protected String getContextPath() {
        return contextPath;
    }

    protected String getFormPath() {
        String formPath = (String) session.getAttribute("form-path");
        if (log.isDebugEnabled()) {
            log.debug("getFormPath() - path = " + formPath);
        }
        return formPath;
    }

    @Override
    protected String makeStartTag() {

        log.debug("doRender() called");
        ServletRequest servletRequest = super.pageContext.getRequest();

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        contextPath = request.getContextPath();
        session = request.getSession();

        String menuId = getMenuId();
        String selectedMenuItemId = getSelected();

        Menu menu = null;
        try {
            menu = getMenu();
            menu.setContextPath(request.getContextPath());
            MenuItem menuItem = menu.findSelectedItemForPath(getFormPath());
            menu.setSelectedItem(menuItem);
        } catch (VectricsException ex) {
            log.error("VectricsException getting menu", ex);
            throw new SystemException("VectricsException getting menu", ex);
        }

        Locale locale = getRequest().getLocale();
        String menuContent = getMenuContent(menu, selectedMenuItemId, locale);

        log.debug("doRender() - finished");
        return menuContent;
    }

    public void setSelected(final String itemId) {
        log.debug("setSelected() - method to set which menu item will be selected");
        selectItemId = itemId;
    }

    public String getSelected() {
        return selectItemId;
    }

    protected abstract String getMenuId();

    public String getMenuContent(final Menu menu, final String selectMenuItemId, final Locale locale) {
        log.debug("getMenuContent() called");
        String menuId = getMenuId();
        if (selectMenuItemId != null && selectMenuItemId.trim().equals("") == false) {
            MenuItem selectedItem = menu.findItemWithId(selectMenuItemId);
            if (selectedItem != null) {
                menu.setSelectedItem(selectedItem);
            } else {
                log.debug("getMenuContent() - could not ind select item with id = " + selectMenuItemId + "  Menu ID = "
                        + menuId);
            }
        }

        StringBuffer page = new StringBuffer();
        page.append("<script language=\"JavaScript\">");

        if (menu != null) {
            page.append("/* Menu Item JavaScript - Created by Vectrics Framework  */ \r\nvar AUTO_MENU_ITEMS_"
                    + menuId.toUpperCase() + " = [\n");
            if (menu.getChildList().size() > 0) {
                for (int i = 0; i < menu.getChildList().size(); i++) {
                    MenuItem item = (MenuItem) menu.getChildList().elementAt(i);
                    log.debug("getMenuContent() - child item.  Id = " + item.getId());
                    log.debug("getMenuContent() - child item.  Description = " + item.getDescription());
                    if (item.getVisible()) {
                        page.append(item.getJavaScript(locale));
                    }
                }
            } else {
                log.debug("getMenuContent() - menu had no items.");
            }
            page.append("];\n");
        } else {
            log.debug("getMenuContent() - no  menu definition found.  Request ignored.");
        }
        page.append("</script>");

        return page.toString();
    }

}
